<?php
include_once 'include.php';

$lid = $_GET['lid'];
$to = $_GET['to'];
$bid = $_GET['bid'];
$cid = $_GET['cid'];
$uid = $_GET['uid'];


$ConfigObj = new GeneralConfig();
$config = $ConfigObj->find(1);
$from = $config->admin_email;

$UnitObj = new Unit();
$BookingObj = new Booking();
$CustomerObj = new Customer();
$SetLettersObj = new SetLetters();

if($to == "GUEST"){
	$guest = $CustomerObj->select_row_query("SELECT `email` FROM `".$CustomerObj->get_table_name()."` WHERE `id` = '$cid'", 1);
	$to = $guest->email;
}
else if($to == "OWNER"){
	$owner = $CustomerObj->select_row_query("SELECT `email` FROM `".$CustomerObj->get_table_name()."` WHERE `id` = (SELECT `owner_id` FROM `".$UnitObj->get_table_name()."` WHERE `id` = '$bookings->uid')", 1);
	$to = $owner->email;
}

$count = $BookingObj->count("SELECT `id` FROM `".$BookingObj->get_table_name()."` WHERE `id` = $bid AND `uid` = $uid AND `cid` = $cid");
if($count > 0){
	$sql = "SELECT t1.id as booking_id, t1.startdate, t1.enddate, t1.total_price, t1.discount, t1.adults, t1.children, t1.notes, t2.name, t3.title as cust_title, t3.firstname, t3.surname, t2.local_phone_number, t2.villa_phone_number, t2.local_contact_name, t2.lockbox_code, t2.driving_direction FROM `".$BookingObj->get_table_name()."` as t1, `".$UnitObj->get_table_name()."` as t2, `".$CustomerObj->get_table_name()."` as t3 WHERE t1.id='$bid' AND t2.id='$uid' AND t3.id='$cid'";
	$replacer = $ConfigObj->select_row_query($sql, 1);
}

$sql = "SELECT * FROM `".$SetLettersObj->get_table_name()."` WHERE `id` = '".$lid."'";
$result = mysql_query($sql);
if(!$row = @mysql_fetch_assoc($result)){
	die("Some error occured. Try again later.");
}
else{
	$subject = $row['msg_subject'];
	$body = $row['msg_body'];
	$body = configure_set_letter($body, $replacer);
}
?>
<html>
<head>
	
</head>
<body>
	<table width="100%" cellpadding="2" cellspacing="2">
	  <tr>
	    <td width="10%"><b>To: </b></td>
	    <td><?php echo $to;?></td>
	  </tr>
	  <tr>
	    <td><b>From: </b></td>
	    <td><?php echo $from;?></td>
	  </tr>
	  <tr>
	    <td><b>Subject: </b></td>
	    <td><?php echo $subject;?></td>
	  </tr>
	  <tr>
	    <td colspan="2"><b>Body</b></td>
	  </tr>
	  <tr>
	    <td colspan="2"><?php echo $body;?></td>
	  </tr>
	  <tr>
	  	<td><input type="button" value="Print This Mail" onclick="window.print()"></td>
	  </tr>
	</table>
</body>
</html>
